package com.dam.monngonmoingay;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class MonNgonDB {
	// DATABASE CONSTANTS
	public final static String DB_NAME = "mnmn.db";
	public final static int DB_VERSION = 3;

	// MonAn TABLE CONSTANTS
	public final static String MONAN_TABLE = "monan_detail";

	public static final String MONAN_ID = "monan_detail_id";
	public static final int MONAN_ID_COL = 0;

	public static final String MONAN_CATALOG_ID = "monan_catalog_id";
	public static final int MONAN_CATALOG_ID_COL = 1;

	public static final String MONAN_TITLE = "title";
	public static final int MONAN_TITLE_COL = 2;

	public static final String MONAN_DESC = "desc";
	public static final int MONAN_DESC_COL = 3;

	public static final String MONAN_CONTENT = "content";
	public static final int MONAN_CONTENT_COL = 4;

	public static final String MONAN_TIPS = "tips";
	public static final int MONAN_TIPS_COL = 5;

	public static final String MONAN_MATERIALS = "materials";
	public static final int MONAN_MATERIALS_COL = 6;

	public static final String MONAN_PATH_VIDEO = "path_video";
	public static final int MONAN_PATH_VIDEO_COL = 7;

	public static final String MONAN_PATH_IMAGE = "path_image";
	public static final int MONAN_PATH_IMAGE_COL = 8;

	public static final String MONAN_LIKE = "like";
	public static final int MONAN_LIKE_COL = 9;

	// Create & Drop MonAn Table
	public static final String CREATE_MONAN_TABLE = "CREATE TABLE monan_detail ("
			+ " monan_detail_id int(11) NOT NULL,"
			+ " monan_catalog_id int(11) NOT NULL DEFAULT '1',"
			+ " title varchar(250) NOT NULL,"
			+ " desc varchar(500) NOT NULL,"
			+ " content varchar(2048) NOT NULL,"
			+ " tips varchar(1024) NOT NULL,"
			+ " materials varchar(1024) NOT NULL,"
			+ " path_video varchar(250) NOT NULL,"
			+ " path_image varchar(250) NOT NULL,"
			+ " PRIMARY KEY (monan_detail_id));";

	public final static String DROP_MONAN_TABLE = "DROP TABLE IF EXISTS "
			+ MONAN_TABLE;

	private static class DBHelper extends SQLiteOpenHelper {
		private static final String DB_DIR = "/data/data/com.dam.monngonmoingay/databases/";
		private static String DB_PATH = DB_DIR + DB_NAME;
		private static String OLD_DB_PATH = DB_DIR + "old_" + DB_NAME;

		private final Context myContext;

		private boolean createDatabase = false;
		private boolean upgradeDatabase = false;

		public DBHelper(Context context, String name, CursorFactory factory,
				int version) {
			super(context, name, factory, version);
			myContext = context;
			DB_PATH = myContext.getDatabasePath(DB_NAME).getAbsolutePath();
			Log.i("abcd", myContext.getDatabasePath(DB_NAME).getAbsolutePath());
		}

		public DBHelper(Context context) {
			super(context, DB_NAME, null, context.getResources().getInteger(
					R.string.databaseVersion));
			myContext = context;
			// Get the path of the database that is based on the context.
			DB_PATH = myContext.getDatabasePath(DB_NAME).getAbsolutePath();
		}

		public void initializeDataBase() {
			getWritableDatabase();

			if (createDatabase) {
				try {
					copyDataBase();
				} catch (IOException e) {
					throw new Error("Error copying database");
				}
			} else if (upgradeDatabase) {
				try {
					FileHelper.copyFile(DB_PATH, OLD_DB_PATH);
					copyDataBase();
					SQLiteDatabase old_db = SQLiteDatabase.openDatabase(
							OLD_DB_PATH, null, SQLiteDatabase.OPEN_READWRITE);
					SQLiteDatabase new_db = SQLiteDatabase.openDatabase(
							DB_PATH, null, SQLiteDatabase.OPEN_READWRITE);
					/*
					 * Add code to load data into the new database from the old
					 * database and then delete the old database from internal
					 * storage after all data has been transferred.
					 */
				} catch (IOException e) {
					throw new Error("Error copying database");
				}
			}

		}

		private void copyDataBase() throws IOException {
			close();
			InputStream myInput = myContext.getAssets().open(DB_NAME);
			OutputStream myOutput = new FileOutputStream(DB_PATH);
			FileHelper.copyFile(myInput, myOutput);

			getWritableDatabase().close();
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			createDatabase = true;
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

			Log.d("NewsRssReader", "Upgrading db from version " + oldVersion
					+ " to " + newVersion);
			upgradeDatabase = true;
		}

		@Override
		public void onOpen(SQLiteDatabase db) {
			super.onOpen(db);
		}
	}

	// database and database helper objects
	private SQLiteDatabase db;
	private DBHelper dbHelper;

	// constructor
	public MonNgonDB(Context context) {
		dbHelper = new DBHelper(context, DB_NAME, null, DB_VERSION);
		dbHelper.initializeDataBase();
	}

	// private methods
	private void openReadableDB() {
		db = dbHelper.getReadableDatabase();
	}

	private void openWriteableDB() {
		db = dbHelper.getWritableDatabase();
	}

	private void closeDB() {
		if (db != null)
			db.close();
	}

	public MonAn getMonAnById(int id) {
		openReadableDB();
		MonAn monan = null;
		String where = MONAN_ID + " = ?";
		String[] whereArgs = { Integer.toString(id) };
		Cursor cursor = db.query(MONAN_TABLE, null, where, whereArgs, null,
				null, null);
		while (cursor.moveToNext()) {
			monan = new MonAn();
			monan.setMonan_detail_id(cursor.getInt(MONAN_ID_COL));
			monan.setMonan_catalog_id(cursor.getInt(MONAN_CATALOG_ID_COL));
			monan.setPath_image(cursor.getString(MONAN_PATH_IMAGE_COL));
			monan.setTitle(cursor.getString(MONAN_TITLE_COL));
			monan.setDesc(cursor.getString(MONAN_DESC_COL));
			monan.setMaterials(cursor.getString(MONAN_MATERIALS_COL));
			monan.setContent(cursor.getString(MONAN_CONTENT_COL));
			monan.setPath_video(cursor.getString(MONAN_PATH_VIDEO_COL));
			monan.setTips(cursor.getString(MONAN_TIPS_COL));
			monan.setLike(cursor.getInt(MONAN_LIKE_COL));
		}
		if (cursor != null)
			cursor.close();
		closeDB();

		return monan;
	}

	public ArrayList<MonAn> getListAll() {
		ArrayList<MonAn> lists = new ArrayList<MonAn>();
		openReadableDB();
		Cursor cursor = db.query(MONAN_TABLE, null, null, null, null, null,
				null);
		while (cursor.moveToNext()) {
			MonAn monan = new MonAn();
			monan.setMonan_detail_id(cursor.getInt(MONAN_ID_COL));
			monan.setMonan_catalog_id(cursor.getInt(MONAN_CATALOG_ID_COL));
			monan.setPath_image(cursor.getString(MONAN_PATH_IMAGE_COL));
			monan.setTitle(cursor.getString(MONAN_TITLE_COL));
			monan.setDesc(cursor.getString(MONAN_DESC_COL));
			monan.setMaterials(cursor.getString(MONAN_MATERIALS_COL));
			monan.setContent(cursor.getString(MONAN_CONTENT_COL));
			monan.setPath_video(cursor.getString(MONAN_PATH_VIDEO_COL));
			monan.setTips(cursor.getString(MONAN_TIPS_COL));
			monan.setLike(cursor.getInt(MONAN_LIKE_COL));
			lists.add(monan);
		}
		if (cursor != null)
			cursor.close();
		closeDB();

		return lists;
	}

	public ArrayList<MonAn> getListById(int id) {
		ArrayList<MonAn> lists = new ArrayList<MonAn>();
		openReadableDB();
		String where = MONAN_CATALOG_ID + " = ?";
		String[] whereArgs = { Integer.toString(id) };
		// Cursor cursor = db.rawQuery("Select * from "+ MONAN_TABLE + " where "
		// +MONAN_CATALOG_ID +" = "+id , null);
		Cursor cursor = db.query(MONAN_TABLE, null, where, whereArgs, null,
				null, null);
		while (cursor.moveToNext()) {
			MonAn monan = new MonAn();
			monan.setMonan_detail_id(cursor.getInt(MONAN_ID_COL));
			monan.setMonan_catalog_id(cursor.getInt(MONAN_CATALOG_ID_COL));
			monan.setPath_image(cursor.getString(MONAN_PATH_IMAGE_COL));
			monan.setTitle(cursor.getString(MONAN_TITLE_COL));
			monan.setDesc(cursor.getString(MONAN_DESC_COL));
			monan.setMaterials(cursor.getString(MONAN_MATERIALS_COL));
			monan.setContent(cursor.getString(MONAN_CONTENT_COL));
			monan.setPath_video(cursor.getString(MONAN_PATH_VIDEO_COL));
			monan.setTips(cursor.getString(MONAN_TIPS_COL));
			monan.setLike(cursor.getInt(MONAN_LIKE_COL));
			lists.add(monan);
		}
		if (cursor != null)
			cursor.close();
		closeDB();

		return lists;
	}

	public ArrayList<MonAn> getListByName(String name) {
		ArrayList<MonAn> lists = new ArrayList<MonAn>();
		openReadableDB();
		String where = MONAN_TITLE + " LIKE ?";
		String[] whereArgs = { name };
		Cursor cursor = db.query(MONAN_TABLE, null, where, whereArgs, null,
				null, null);
		while (cursor.moveToNext()) {
			MonAn monan = new MonAn();
			monan.setMonan_detail_id(cursor.getInt(MONAN_ID_COL));
			monan.setMonan_catalog_id(cursor.getInt(MONAN_CATALOG_ID_COL));
			monan.setPath_image(cursor.getString(MONAN_PATH_IMAGE_COL));
			monan.setTitle(cursor.getString(MONAN_TITLE_COL));
			monan.setDesc(cursor.getString(MONAN_DESC_COL));
			monan.setMaterials(cursor.getString(MONAN_MATERIALS_COL));
			monan.setContent(cursor.getString(MONAN_CONTENT_COL));
			monan.setPath_video(cursor.getString(MONAN_PATH_VIDEO_COL));
			monan.setTips(cursor.getString(MONAN_TIPS_COL));
			monan.setLike(cursor.getInt(MONAN_LIKE_COL));
			lists.add(monan);
		}
		if (cursor != null)
			cursor.close();
		closeDB();

		return lists;
	}

	public ArrayList<MonAn> getListByLike() {
		ArrayList<MonAn> lists = new ArrayList<MonAn>();
		openReadableDB();
		String where = MONAN_LIKE + " = ?";
		String[] whereArgs = { Integer.toString(1) };
		Cursor cursor = db.query(MONAN_TABLE, null, where, whereArgs, null,
				null, null);
		while (cursor.moveToNext()) {
			MonAn monan = new MonAn();
			monan.setMonan_detail_id(cursor.getInt(MONAN_ID_COL));
			monan.setMonan_catalog_id(cursor.getInt(MONAN_CATALOG_ID_COL));
			monan.setPath_image(cursor.getString(MONAN_PATH_IMAGE_COL));
			monan.setTitle(cursor.getString(MONAN_TITLE_COL));
			monan.setDesc(cursor.getString(MONAN_DESC_COL));
			monan.setMaterials(cursor.getString(MONAN_MATERIALS_COL));
			monan.setContent(cursor.getString(MONAN_CONTENT_COL));
			monan.setPath_video(cursor.getString(MONAN_PATH_VIDEO_COL));
			monan.setTips(cursor.getString(MONAN_TIPS_COL));
			monan.setLike(cursor.getInt(MONAN_LIKE_COL));
			lists.add(monan);
		}
		if (cursor != null)
			cursor.close();
		closeDB();

		return lists;
	}
	
	public void Like(String title) {
		String where = MONAN_TITLE + " = ?";
		String[] whereArgs = { title };
		ContentValues cv = new ContentValues();
		cv.put(MONAN_LIKE, 1);
		Log.e("title", title);
		openWriteableDB();
		db.update(MONAN_TABLE, cv, where, whereArgs);
	}
	
	public void UnLike(String title) {
		String where = MONAN_TITLE + " = ?";
		String[] whereArgs = { title };
		ContentValues cv = new ContentValues();
		cv.put(MONAN_LIKE, 0);
		openWriteableDB();
		db.update(MONAN_TABLE, cv, where, whereArgs);
	}
}
